<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="./data.js"></script>
</head>
<body>

<script>

    let treeNos = [] // 编号
    let vehicleOptions = []  // 车辆数据

    // 处理编号问题
    function calcTreeNo() {
        treeNos.push(treeNos.length ? treeNos.length : 0)
        return treeNos.length
    }

    // 开始解析
    function parseTreeData(data) {
        for (let i = 0, len = data.length; i < len; i++) {
            // 组织数据
            if (data[i]['children']) {
                data[i].treeId = `vehicle${calcTreeNo()}`
                if (data[i].vehicles) {
                    for (let v of data[i].vehicles) {
                        v.treeId = `vehicle${calcTreeNo()}`
                    }
                    vehicleOptions = vehicleOptions.concat(data[i].vehicles)
                    data[i]['children'] = data[i]['children'].concat(data[i].vehicles)
                }
                parseTreeData(data[i]['children'])  // 对每一条数据下的children进行解析
            } else if (data[i].vehicles) {
                //车辆数据
                for (let v of data[i].vehicles) {
                    v.treeId = `vehicle${calcTreeNo()}`
                }
                vehicleOptions = vehicleOptions.concat(data[i].vehicles)
                data[i]['children'] = data[i].vehicles
            }
        }
        return data
    }

    console.log('树型结构:', vehicleData)
    parseTreeData(vehicleData)
    console.log('解析后:', vehicleOptions)
</script>
</body>
</html>
